import axios from 'axios';

// 创建axios实例
const api = axios.create({
    baseURL: 'http://localhost:8080/api',
    timeout: 5000
});

// 请求拦截器
api.interceptors.request.use(
    config => {
        const token = localStorage.getItem('token');
        if (token) {
            config.headers['Authorization'] = `Bearer ${token}`;
        }
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

// 响应拦截器
api.interceptors.response.use(
    response => {
        return response.data;
    },
    error => {
        if (error.response) {
            switch (error.response.status) {
                case 401:
                    // 未登录或token过期
                    localStorage.removeItem('token');
                    window.location.href = '/login.html';
                    break;
                default:
                    alert(error.response.data.msg || '系统错误');
            }
        }
        return Promise.reject(error);
    }
);

// 登录接口
export const login = (data) => {
    return api.post('/login', data);
};

// 获取教师列表
export const getTeachers = (params) => {
    return api.get('/getTeachers', { params });
};

// 添加教师
export const addTeacher = (data) => {
    return api.post('/addTeacher', data);
};

// 更新教师信息
export const updateTeacher = (data) => {
    return api.post('/updateTeacher', data);
};

// 删除教师
export const deleteTeacher = (id) => {
    return api.post('/deleteTeacher', { id });
}; 